import request from "@/request";

// 响应接口
export interface GenerateImageCodeRes {
}

/**
 * 生成验证码
 * @returns
 */
export function generateImageCode(): Promise<GenerateImageCodeRes> {
    return request.get(`/userEntity/generateImageCode`, {
        responseType: "arraybuffer",
    });
}

// 参数接口
export interface SaveParams {
    id: number;
    userId: string;
    passwd: string;
    username: string;
    gender: number;
    birth?: Record<string, unknown>;
    roleId: number;
    state: number;
}

// 响应接口
export interface SaveRes {
    message: string;
    success: boolean;
    code: number;
    data: Record<string, unknown>;
}

/**
 * 保存或修改
 * @param {object} params 保存用户参数
 * @param {number} params.id 序号
 * @param {string} params.userId 用户账号
 * @param {string} params.passwd 密码
 * @param {string} params.username 名称
 * @param {number} params.gender 性别
 * @param {object} params.birth 生日
 * @param {number} params.roleId 权限
 * @param {number} params.state 状态
 * @returns
 */
export function save(params: SaveParams): Promise<SaveRes> {
    return request.post(`/userEntity/save`, params);
}

// 参数接口
export interface LoginParams {
    userId: string;
    passwd: string;
    code?: string;
}

// 响应接口
export interface LoginRes {
    message: string;
    success: boolean;
    code: number;
    data: Record<string, unknown>;
}

/**
 * 用户登录
 * @param {object} params 登录参数
 * @param {string} params.userId 用户账号
 * @param {string} params.passwd 密码
 * @param {string} params.code 验证码
 * @returns
 */
export function login(params: LoginParams): Promise<LoginRes> {
    return request.post(`/userEntity/login`, params);
}

// 参数接口
export interface ExistParams {
    id?: number;
}

// 响应接口
export interface ExistRes {
    message: string;
    success: boolean;
    code: number;
    data: Record<string, unknown>;
}

/**
 * 是否存在
 * @param {object} params IdParam
 * @param {number} params.id ID，记录唯一标识
 * @returns
 */
export function exist(params: ExistParams): Promise<ExistRes> {
    return request.post(`/userEntity/exist`, params);
}

// 参数接口
export interface DetailParams {
    id?: number;
}

// 响应接口
export interface DetailRes {
    message: string;
    success: boolean;
    code: number;
    data: {
        id: number;
        userId: string;
        passwd: string;
        username: string;
        gender: number;
        birth: Record<string, unknown>;
        roleId: number;
        state: number;
        createTime: Record<string, unknown>;
        updateTime: Record<string, unknown>;
        roleEntity: {
            id: number;
            icon: string;
            label: string;
            power: number;
            limitBook: number;
            limitDay: number;
            state: number;
            createTime: Record<string, unknown>;
            updateTime: Record<string, unknown>;
        };
    };
}

/**
 * 根据id查找
 * @param {object} params IdParam
 * @param {number} params.id ID，记录唯一标识
 * @returns
 */
export function detail(params: DetailParams): Promise<DetailRes> {
    return request.post(`/userEntity/detail`, params);
}

// 响应接口
export interface ListRes {
    message: string;
    success: boolean;
    code: number;
    data: {
        id: number;
        userId: string;
        passwd: string;
        username: string;
        gender: number;
        birth: Record<string, unknown>;
        roleId: number;
        state: number;
        createTime: Record<string, unknown>;
        updateTime: Record<string, unknown>;
    }[];
}

/**
 * 用户列表
 * @returns
 */
export function list(): Promise<ListRes> {
    return request.get(`/userEntity/list`);
}

// 参数接口
export interface DeleteParams {
    id?: number;
}

// 响应接口
export interface DeleteRes {
    message: string;
    success: boolean;
    code: number;
    data: Record<string, unknown>;
}

/**
 * 根据id删除
 * @param {object} params IdParam
 * @param {number} params.id ID，记录唯一标识
 * @returns
 */
// export function delete(params: DeleteParams): Promise<DeleteRes> {
//     return request.post(`/userEntity/delete`, params);
// }



